package com.cennavi.bigdata.baotou.scripts;

import org.apache.commons.io.FileUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BaotouBaseData {
	
	public static Map<String,String> mapDir = new HashMap<String,String>();

	//用来过滤重复id
	public static Map<String,String> mapFilter = new HashMap<String,String>();
	
	public static Map<String,String> rticCenterMap = new HashMap<String,String>();
	
	/*
		东 E
		西 W
		南 S
		北 N
	 */
	static{
		mapDir.put("东向西", "E-W");
		mapDir.put("西向东", "W-E");
		mapDir.put("南向北", "S-N");
		mapDir.put("北向南", "N-S");
	}

	//comm_seg_info_new
	//id、area、roadname、kind、roaddir、dir、startname、endname、length、picNum
	//comm_seg_rtic_new
	//id、rtic、kind、latlon、latlonlist
	/*上面的文件，第一次是没有的，需要先拿Segment_Merge_baotou.csv文件跑出来一版，然后入库，找出重复的道路ID，
	然后放到repetition.txt文件中，然后在拿Segment_Merge_baotou.csv文件跑出来一版，
	这版跑的时候就会直接筛选掉那些重复道路ID的 */
	//Segment_Merge_baotou.csv这个是路段信息表
	public static void main(String[] args) throws Exception {
		List<String> seg_info = new ArrayList<String>();
		List<String> seg_rtic = new ArrayList<String>();
		List<String> seg_info_sql = new ArrayList<String>();
		List<String> seg_rtic_sql = new ArrayList<String>();
		readFilter();
		readRticCenter();
		BufferedReader reader = new BufferedReader(new FileReader("D:\\项目\\包头资料\\更新升级\\2018-10-11\\Segment_Merge_baotou.csv"));
		String line = "";
		while ((line = reader.readLine()) != null) {
			String[] str = line.replace("\"", "").split(",");
			StringBuffer infoSB = new StringBuffer();
			StringBuffer insertINFO = new StringBuffer();
			
			String roadname = str[1] + str[5] + str[6];
			String pinyin = getRoadNamePinyin(roadname);
			if (!mapFilter.containsKey(pinyin)) {
				// id
				infoSB.append(pinyin).append(",");
				// area（城市代码）
				infoSB.append("150200").append(",");
				// roadname（道路名称）
				infoSB.append(str[1]).append(",");
				// kind（道路等级）
				infoSB.append(str[7].substring(6, 7)).append(",");
				// roaddir
				infoSB.append("0").append(",");
				// dir（方向）
				infoSB.append(str[3]).append(",");
				// startname（路段开始道路名）
				infoSB.append(str[5]).append(",");
				// endname（路段结束道路名）
				infoSB.append(str[6]).append(",");
				// length（道路长度，匹配表是千米，转换成米）
				String length = String.valueOf(new Double(Double
						.parseDouble(str[8]) * 1000).intValue());
				infoSB.append(length).append(",");
				// picNum（主路还是支路）
				infoSB.append("");
				//rticid
				String[] rtics = str[7].split(" ");
				//坐标
				String[] rticLonlats = str[9].split(" ");
				for (int i = 0; i < rtics.length; i++) {
					StringBuffer rticSB = new StringBuffer();
					StringBuffer insertRTIC = new StringBuffer();
					// id
					rticSB.append(getRoadNamePinyin(roadname)).append(",");
					// rtic
					rticSB.append(rtics[i]).append(",");
					// kind
					rticSB.append(str[2]).append(",");
					// latlon
					String[] rticlonlat = rticLonlats[i].split(";");
					rticSB.append(rticlonlat[0] + "," + rticlonlat[1])
							.append(",");
					// latlonlist
					rticSB.append("");
					seg_rtic.add(rticSB.toString());

					String insertINFOSQL = "INSERT INTO comm_seg_rtic(id,rtic,kind,latlon,latlonlist) VALUES(";
					insertRTIC.append(insertINFOSQL);
					// id
					insertRTIC.append("'").append(getRoadNamePinyin(roadname))
							.append("',");
					// rtic
					insertRTIC.append("'").append(rtics[i]).append("',");
					// kind
					insertRTIC.append("").append(str[7].substring(6, 7))
							.append(",");
					// latlon
					String lonlat = rticCenterMap.get(rtics[i]);
					insertRTIC.append("'")
							.append(lonlat)
							.append("',");
//					insertRTIC.append("'")
//							.append(rticlonlat[0] + "," + rticlonlat[1])
//							.append("',");
					// latlonlist
					insertRTIC.append("''");
					insertRTIC.append(");");
					seg_rtic_sql.add(insertRTIC.toString());
				}
				seg_info.add(infoSB.toString());
				String insertINFOSQL = "INSERT INTO comm_seg_info(id,AREA,roadname,kind,roaddir,dir,startname,endname,LENGTH,picNum) VALUES(";
				insertINFO.append(insertINFOSQL);
				// id
				insertINFO.append("'").append(getRoadNamePinyin(roadname))
						.append("',");
				// area
				insertINFO.append("").append("150200").append(",");
				// roadname
				insertINFO.append("'").append(str[1]).append("',");
				// kind
				insertINFO.append("").append(str[7].substring(6, 7))
						.append(",");
				// roaddir
				insertINFO.append("").append("0").append(",");
				// dir
				insertINFO.append("'").append(str[3]).append("',");
				// startname
				insertINFO.append("'").append(str[5]).append("',");
				// endname
				insertINFO.append("'").append(str[6]).append("',");
				// length
				insertINFO.append("").append(length).append(",");
				// picNum
				insertINFO.append("''");
				insertINFO.append(");");
				seg_info_sql.add(insertINFO.toString());
			}
		}
		FileUtils.writeLines(new File("D:\\11pan\\baotou\\baotouNew20181012\\comm_seg_info_new.csv"), seg_info, true);
		FileUtils.writeLines(new File("D:\\11pan\\baotou\\baotouNew20181012\\comm_seg_info_SQL.sql"), seg_info_sql, true);
		
		FileUtils.writeLines(new File("D:\\11pan\\baotou\\baotouNew20181012\\comm_seg_rtic_new.csv"), seg_rtic, true);
		FileUtils.writeLines(new File("D:\\11pan\\baotou\\baotouNew20181012\\comm_seg_rtic_SQL.sql"), seg_rtic_sql, true);
		reader.close();
	}
	
	public static void readFilter(){
		try {
			BufferedReader reader = new BufferedReader(
					new FileReader("D:\\11pan\\baotou\\baotouNew20181012\\repetition.txt"));
			String line = "";
			while ((line = reader.readLine()) != null) {
				mapFilter.put(line, line);
			}
			reader.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void readRticCenter(){
		try {
			/*BufferedReader reader = new BufferedReader(
					new FileReader("E:\\11pan\\baotou\\baotouNew20180421\\baotouRticCenter.mid"));
			String line = "";
			while ((line = reader.readLine()) != null) {
				String[] str = line.replace("\"", "").split(",");
				String key = str[0];
				String value = str[1] + "," + str[2];
				rticCenterMap.put(key, value);
			}
			reader.close();*/
            BufferedReader reader = new BufferedReader(
                    new FileReader("D:\\项目\\包头资料\\更新升级\\2018-10-11\\Segment_Merge_baotou.csv"));
            String line = "";
            while ((line = reader.readLine()) != null) {
                String[] str = line.replace("\"", "").split(",");
                String[] splitRtic = str[7].split(" ");//rtic数组
                String[] splitLont = str[9].split(" ");//坐标信息
                for (int i = 0; i < splitRtic.length; i++) {
                    rticCenterMap.put(splitRtic[i], splitLont[i].split(";")[0]+","+splitLont[i].split(";")[1]);
                }
            }
            reader.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static String getRoadNamePinyin(String roadName) {
		String roadNames = roadName;
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < roadNames.length(); i++) {
			sb.append(HanZiToPinYin.toPinYin(roadNames.charAt(i)));
		}
		return sb.toString();
	}
}
